const app = getApp()

Page({
  data: {
    chat_user_id: 0,
    send_content: '',
    list: [],
    statusbar: 0,
    user_info:{}
  },
  onLoad: function (t) {
    getApp().page.onLoad(this, t);
    if (t.chat_user) {
      this.setData({
        chat_user_id: t.chat_user
      });
    }
    this.loadData()
    const res = wx.getSystemInfoSync()
    var statusbarH = res.statusBarHeight
    this.setData({
      statusbar: statusbarH
    })
  },
  loadData(){
    var t = this;
    getApp().core.showLoading({}), getApp().request({
      url: getApp().api.weitao.user_chat,
      data: {
        chat_user_id: t.data.chat_user_id
      },
      method: "get",
      success: function (e) {
        getApp().core.hideLoading()
        if (0 == e.code) {
           t.setData({
            list: e.data.list,
            user_info: e.data.user_info
          });
        } else getApp().core.showToast({
          title: e.msg,
          icon: 'none'
        });
      },
      fali: function (t) {
        getApp().core.hideLoading()
        getApp().core.showToast({
          title: t.msg,
          icon: 'none'
        });
      }
    });
  },
  inputChange(e){
    this.setData({
      send_content: e.detail.value
    });
  },
  //跳转页面
  goUrl: function (e) {
    var gopage = e.currentTarget.dataset.url;
    if (!gopage) {
      return;
    }
    getApp().core.navigateTo({
      url: gopage
    });
  },
  goBack(){
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('someEvent', 1);
    wx.navigateBack({
      delta: 1
    })
  },
  send(){
    var a = this;
    if(!a.data.send_content){
      getApp().core.showToast({
        title: "发送内容不能为空",
        icon: 'none'
      });
      return;
    }
    getApp().core.showLoading({
        title: "加载中"
    }), getApp().request({
        url: getApp().api.weitao.send_user_chat,
        data: {
          chat_user_id: a.data.chat_user_id,
          send_content: a.data.send_content
        },
        success: function(t) {
            0 == t.code && (
              a.setData({
                send_content: ''
              }),a.loadData());
            getApp().core.showToast({
              title: t.msg,
              icon: 0 == t.code ? 'success' : 'none'
            });
        },
        complete: function() {
            getApp().core.hideLoading();
        }
    });
  }
})
